package com.hpe.wx.performance.mongo.task;

import java.io.IOException;
import java.util.List;

import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.ReflectionException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hpe.wx.performance.mongo.jmxObj.domain.JmxObj;
import com.hpe.wx.performance.mongo.jmxObj.service.JmxObjService;
import com.hpe.wx.performance.mongo.statusrecord.service.StatusRecordService;

@Component
public class GetJMXDataMongo {
	private static Logger logger = Logger.getLogger(GetJMXDataMongo.class.getSimpleName());
	@Autowired
	private StatusRecordService service_sr;
	
	@Autowired
	private JmxObjService service_jo;
	
	//@Scheduled(cron="0/5 * *  * * ? ")
    public void taskCycle(){
        System.out.println("=========================================aaaaaaaaa");
    }

	/**
	 * heartbeat, it will execute per 30 seconds, to get data from jmx and store into redis
	 * if there is not any jmx or just for test, comment it  
	 */
	//@Scheduled(fixedRate = 1000 * 20)
	public void getJMXData() {
		List<JmxObj> urls = service_jo.getByStatus(1);
		for(JmxObj tup : urls){ 
			String jmxObjId = tup.getId();
			logger.info("The jmxObjId for get info from remote machine is: " + jmxObjId);
			try {
				service_sr.addARecord2DB(jmxObjId);
			}  catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				continue;
			}
		}
	}
	
}
